import { getToken, setToken, removeToken } from '@/utils/auth'
import { login, getUserInfo } from '@/api/user'
import { constantRoutes } from '@/router'
import { resetRouter } from '@/router'
export default {
  namespaced: true,
  state: {
    token: getToken(),
    userInfo: {
    },
    routes: constantRoutes
  },
  mutations: {
    setToken(state, token) {
      state.token = token
      // console.log(state.token)
      setToken(token)
    },
    removeToken(state) {
      state.token = null
      removeToken()
    },
    setUserInfo(state, userInfo) {
      state.userInfo = userInfo
      console.log(state.userInfo)
    },
    removeUserInfo(state) {
      state.userInfo = null
    },
    setRoutes(state, routes) {
      state.routes = [...constantRoutes, ...routes]
    }
  },
  actions: {
    async login(context, form) {
      // 调用登陆接口 返回token
      const token = await login(form.phone, form.password)
      context.commit('setToken', token)
    },
    async getUserInfo(context) {
      // 调用获取用户信息接口
      const userInfo = await getUserInfo()
      // console.log(userInfo)
      context.commit('setUserInfo', userInfo)
      return userInfo
    },
    logout(context) {
      context.commit('removeToken')
      context.commit('removeUserInfo')
      resetRouter()
    }

  },
  getters: {}
}
